package prefix_sum;

public class pivotIndex {
    public int pivotIndex(int[] nums) {

        int n = nums.length;
        int[] pretab = new int[n];
        int[] afttab = new int[n];
        for(int i =1;i <= n-1;i++) {//前缀和表
            pretab[i] = pretab[i-1] + nums[i-1];
        }
        for(int i = n-2;i >= 0;i--) {//后缀和表
            afttab[i] = afttab[i+1]+nums[i+1];
        }

        for(int i = 0;i < n;i++) {
            if(pretab[i] == afttab[i]) {
                return i;
            }
        }
        return -1;

    }

}
